package com.cjq.controller;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cjq.domain.User;
import com.cjq.service.UserService;
import com.cjq.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;

/**
 * @author junqiancai@foxmail.com
 * @since 2021/6/14 19:05
 * 管理员查询打卡记录
 */
@RestController
public class ClockController {
    @Autowired
    private UserService userService;

    @PreAuthorize("hasAnyRole('ADMIN','SUPER')")
    @PostMapping("/clock")
    public Result getClockState(@RequestParam("isFinished") Integer isFinished,
                                @RequestParam(value = "pageSize",required = false,defaultValue = "10") Integer pageSize,
                                @RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum,
                                @RequestBody(required = false) User user,
                                @RequestParam(value = "date",required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Date date)
    {
        if(date==null)date = new Date();
        java.sql.Date targetDate = new java.sql.Date(date.getTime());
        final IPage<User> userIPage;
        final Page<User> page = new Page<>(pageNum, pageSize);
        Integer code =Result.SUCCESS;
        if(isFinished==1){
            userIPage = userService.selectClockByCondition(page, targetDate, user);
        }
        else {
            userIPage = userService.selectNoClockByCondition(page, targetDate, user);
        }
        if(userIPage.getRecords().size()==0){   //没有内容
            code = Result.NO_CONTENT;
        }
        return Result.succeed(code,"查询成功",userIPage);
    }

    @PreAuthorize("hasAnyRole('ADMIN','SUPER')")
    @PostMapping("/clock/download")
    public void downloadClockState(@RequestParam("isFinished") Integer isFinished,
                                     @RequestBody(required = false) User user,
                                     @RequestParam(value = "date",required = false)
                                         @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Date date,
                                     HttpServletResponse response) throws IOException {
        if(date==null)date = new Date();
        java.sql.Date targetDate = new java.sql.Date(date.getTime());
        List<User> users;
        if(isFinished==1){
            users = userService.selectClockByCondition(targetDate, user);
        }
        else {
            users = userService.selectNoClockByCondition(targetDate, user);
        }
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("测试", "UTF-8").replaceAll("\\+", "%20");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), User.class).sheet("模板").doWrite(users);
    }


}
